﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Configuration;
using System.Web.Caching;

namespace Sueetie.Core
{

    #region Configuration Class Objects

    public class SueetieProvider
    {
        public string Name { get; set; }
        public string Function { get; set; }
        public string ConnectionString { get; set; }
        public string ProviderType { get; set; }
    }

    public class AvatarSetting
    {
        public int Height { get; set; }
        public int Width { get; set; }
        public int ThumbnailHeight { get; set; }
        public int ThumbnailWidth { get; set; }
        public int Size { get; set; }
        public string AvatarFolderPath { get; set; }
        public int ImageQuality { get; set; }
    }

    #endregion

    public class SueetieConfiguration
    {

        #region internal properties

        private static readonly string configKey = "SueetieConfig";
        private static readonly object configLocker = new object();
        private XDocument configXML = null;

        #endregion

        #region Configuration Public Properties

        public List<SueetieProvider> SueetieProviders { get; set; }
        public AvatarSetting AvatarSettings { get; set; }
        public string ConfigPath { get; set; }
        public string SiteRootPath { get; set; }

        #endregion

        #region Constructor

        public SueetieConfiguration(XDocument doc)
        {
            configXML = doc;
            ConfigPath = ConfigurationManager.AppSettings["rootpath"].ToString() + "Sueetie.config";
            SiteRootPath = ConfigurationManager.AppSettings["rootpath"].ToString();
            PopulateProviders();
            PopulateAvatarSetting();
        }


        #endregion

        #region Get()

        public static SueetieConfiguration Get()
        {
            SueetieConfiguration config = SueetieCache.Current[configKey] as SueetieConfiguration;
            if (config == null)
            {
                lock (configLocker)
                {
                    config = SueetieCache.Current[configKey] as SueetieConfiguration;
                    if (config == null)
                    {
                        string configPath = ConfigurationManager.AppSettings["rootpath"].ToString() + "Sueetie.config";
                        XDocument doc = XDocument.Load(configPath);

                        config = new SueetieConfiguration(doc);
                        SueetieCache.Current.InsertMax(configKey, config, new CacheDependency(configPath));
                    }
                }
            }
            return config;

        }

        #endregion

        #region Populate Class Objects

        private void PopulateProviders()
        {

            var providers = from provider in configXML.Descendants("Provider")
                            select new SueetieProvider
                            {
                                ConnectionString = (string)provider.Element("connectionString"),
                                Name = (string)provider.Element("name"),
                                Function = (string)provider.Element("function"),
                                ProviderType = (string)provider.Element("type")
                            };
            SueetieProviders = providers.ToList();

        }

        private void PopulateAvatarSetting()
        {
            var avatarsettings = from avatarsetting in configXML.Descendants("AvatarSettings")
                                   select new AvatarSetting
                                 {
                                     Height = (int)avatarsetting.Attribute("Height"),
                                     Width = (int)avatarsetting.Attribute("Width"),
                                     ThumbnailHeight = (int)avatarsetting.Attribute("ThumbnailHeight"),
                                     ThumbnailWidth = (int)avatarsetting.Attribute("ThumbnailWidth"),
                                     Size = (int)avatarsetting.Attribute("Size"),
                                     AvatarFolderPath = (string)avatarsetting.Attribute("AvatarFolderPath"),
                                     ImageQuality = (int)avatarsetting.Attribute("ImageQuality")
                                 };
                                 
            AvatarSettings = avatarsettings.Single();
        }

        #endregion

    }

}
